﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using YunSpace.Core;
using YunSpace.Domain.Entity.MiniSite;
using YunSpace.Domain.IRepository.MiniSite;
using YunSpace.Repository.MiniSite;
using System.Collections.Generic;
using System.Linq;
using YunSpace.ThirdService;
using System;
#endregion

namespace YunSpace.Application.MiniSite
{
    public class SmsInfoApp
    {
        private ISmsInfoRepository service = new SmsInfoRepository();

        public List<SmsInfoEntity> GetList(Pagination pagination, string queryJson)
        {
            var expression = ExtLinq.True<SmsInfoEntity>();
            var queryParam = queryJson.ToJObject();
            // if (!queryParam["keyword"].IsEmpty())
            // {
            //     string keyword = queryParam["keyword"].ToString();
            //     expression = expression.And(t => t.F_Account.Contains(keyword));
            // }
            // if (!queryParam["timeType"].IsEmpty())
            // {
            //     string timeType = queryParam["timeType"].ToString();
            //     DateTime startTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate();
            //     DateTime endTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate().AddDays(1);
            //     switch (timeType)
            //     {
            //         case "1":
            //             break;
            //         case "2":
            //             startTime = DateTime.Now.AddDays(-7);
            //             break;
            //         case "3":
            //             startTime = DateTime.Now.AddMonths(-1);
            //             break;
            //         case "4":
            //             startTime = DateTime.Now.AddMonths(-3);
            //             break;
            //         default:
            //             break;
            //     }
            //     expression = expression.And(t => t.F_Date >= startTime && t.F_Date <= endTime);
            // }
            return service.FindList(expression, pagination);
        }

        public SmsInfoEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void SubmitForm(SmsInfoEntity smsinfoEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                smsinfoEntity.Modify(keyValue);
                service.Update(smsinfoEntity);
            }
            else
            {
                smsinfoEntity.Create();
                // smsinfoEntity.F_Category = 2;
                service.Insert(smsinfoEntity);
            }
        }

        /// <summary>
        /// 生成短信验证码
        /// </summary>
        public bool ValidCode(string phone)
        {

            var smsMgr = new RongLianSmsManger();
            var code = smsMgr.RandomValidCode();

            var data = smsMgr.SendValidCode(phone, code);

            var result = !data.Contains("失败");
            if (result)
            {
                var smsinfoEntity = new SmsInfoEntity()
                {
                    F_Phone = phone,
                    F_ValidCode = code,
                    F_Result = data,
                    F_Type = 1
                };
                smsinfoEntity.Create();

                service.Insert(smsinfoEntity);
            }

            return result;
        }


        /// <summary>
        /// 随机密码
        /// </summary>
        public bool ForgetPassword(string phone)
        {
            var app = new CustomerApp();
            var newPassword = app.RandomPassowrd(phone);

            if (phone.IsEmpty() || newPassword.IsEmpty()) return false;
            
            var smsMgr = new RongLianSmsManger();
            var data = smsMgr.SendModifyPassord(phone, newPassword);

            var result = !data.Contains("失败");
            if (result)
            {
                var smsinfoEntity = new SmsInfoEntity()
                {
                    F_Phone = phone,
                    F_ValidCode = newPassword,
                    F_Result = data,
                    F_Type = 2
                };
                smsinfoEntity.Create();

                service.Insert(smsinfoEntity);
            }

            return result;
        }

        /// <summary>
        /// 短信验证码是否过期
        /// </summary>
        public bool IsExpire(SmsInfoEntity smsInfoEntity) {
            var currentDate = DateTime.Now.AddMinutes(-10);
            var result = service.FindEntity(p => p.F_Phone == smsInfoEntity.F_Phone && p.F_ValidCode == smsInfoEntity.F_ValidCode && p.F_CreatorTime >= currentDate);

            return result == null;
        }
        
    }
}

